package com.lfy.yunpicture.manager.websocket.disruptor;

import com.lfy.yunpicture.manager.websocket.model.PictureEditEvent;
import com.lfy.yunpicture.manager.websocket.model.PictureEditRequestMessage;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;

/**
 * 图片编辑事件生产者
 */
@Component
public class PictureEditEventProducer {
    @Resource
    private Disruptor<PictureEditEvent> disRuptor;
    public void pictureEditEventPublish(PictureEditRequestMessage pictureEditRequestMessage, Long pictureId, WebSocketSession session){
        RingBuffer<PictureEditEvent> ringBuffer = disRuptor.getRingBuffer();
        // 获取可以写入的下标
        long next = ringBuffer.next();
        PictureEditEvent pictureEditEvent = ringBuffer.get(next);
        pictureEditEvent.setPictureEditRequestMessage(pictureEditRequestMessage);
        pictureEditEvent.setPictureId(pictureId);
        pictureEditEvent.setWebSocketSession(session);
        // 发布事件
        ringBuffer.publish(next);
    }
}
